package com.xc.content.exception;

import com.xc.content.entity.ErrorResponse;
import com.xc.content.enums.HttpStatus;
import com.xc.content.utils.ResponseResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author: hyacinth
 * @CreateTime: 2025-01-03
 * @Version: 1.0
 */
@ControllerAdvice // 控制器增强
@Slf4j
public class GlobalExceptionHandler {

    @ExceptionHandler(value = BaseException.class)
    @ResponseBody
    public ResponseResult<ErrorResponse> handleBaseException(BaseException ex, HttpServletRequest request) {
        log.error("业务异常： {} | 请求路径: {}", ex.getMessage(), request.getRequestURI(), ex);
        return createErrorResponse(ex.getErrorCode(), ex.getMessage(), ex.getData());
    }

    @ExceptionHandler(value = MethodArgumentNotValidException.class)
    @ResponseBody
    public ResponseResult<ErrorResponse> handleValidationException(MethodArgumentNotValidException ex, HttpServletRequest request) {
        log.error("参数校验失败： {} | 请求路径: {}", ex.getMessage(), request.getRequestURI(), ex);
        String errorMessage = ex.getBindingResult().getFieldErrors().get(0).getDefaultMessage();
        return createErrorResponse(HttpStatus.BAD_REQUEST.getValue(), errorMessage, null);
    }

    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public ResponseResult<ErrorResponse> handleAllExceptions(Exception ex, HttpServletRequest request) {
        log.error("系统异常： {} | 请求路径: {}", ex.getMessage(), request.getRequestURI(), ex);
        return createErrorResponse(HttpStatus.SYS_EXCEPTION.getValue(), "系统异常，请稍后重试", null);
    }

    @ExceptionHandler(value = DatabaseException.class)
    @ResponseBody
    public ResponseResult<ErrorResponse> handleDatabaseException(DatabaseException ex, HttpServletRequest request) {
        log.error("数据库异常： {} | 请求路径: {}", ex.getMessage(), request.getRequestURI(), ex);
        return createErrorResponse(HttpStatus.DB_EXCEPTION.getValue(), "数据库操作失败，请稍后重试", ex.getData());
    }

    @ExceptionHandler(value = HttpRequestMethodNotSupportedException.class)
    @ResponseBody
    public ResponseResult<ErrorResponse> handleHttpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException ex, HttpServletRequest request) {
        log.error("请求方法不支持异常： {} | 请求路径: {}", ex.getMessage(), request.getRequestURI(), ex);
        return createErrorResponse(HttpStatus.METHOD_NOT_ALLOWED.getValue(), "请求方法不支持", null);
    }

    @ExceptionHandler(value = MissingServletRequestParameterException.class)
    @ResponseBody
    public ResponseResult<ErrorResponse> handleMissingServletRequestParameterException(MissingServletRequestParameterException ex, HttpServletRequest request) {
        log.error("请求参数缺失异常： {} | 请求路径: {}", ex.getMessage(), request.getRequestURI(), ex);
        return createErrorResponse(HttpStatus.BAD_REQUEST.getValue(), "请求参数缺失", null);
    }

    private ResponseResult<ErrorResponse> createErrorResponse(int code, String message, Object data) {
        ErrorResponse errorResponse = new ErrorResponse();
        errorResponse.setCode(code);
        errorResponse.setMessage(message);
        errorResponse.setData(data);
        ResponseResult<ErrorResponse> result = new ResponseResult<>();
        result.setCode(code);
        result.setData(errorResponse);
        result.setMessage(message);
        return result;
    }
}
